<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Option Enter Text Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  input { width: 20px; }
  /* 160 + 80 + 50 + 10 */
  #wrap,
  #sync-task-cover {
    height: 300px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 54px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds

var createElements = function(div){
  var n = core.randi(2, 7);
  var clickNames = [];
  for(var i=0;i<n;i++) {
    var chname = ui_utils.generateString(2,8);
    var label = div.append('label')
    label.append('input').attr('type', 'radio').attr('id', 'ch'+i).attr('name', 'radio');
    label[0][0].innerHTML += chname;
    div.append('br');
    clickNames.push(chname);
  }
  var ix = core.randi(0, n);
  var qstr = clickNames[ix];
  return {query: qstr, index: ix};
}

// enter-text
var randomizeInputs = function(){
  // move the text field around
  var s = '';
  s += 'margin-left:'+core.randi(0,20)+'px;';
  s += 'margin-top:'+core.randi(0,20)+'px;';
  d3.select('#form').attr('style', s);

  // and submit button a bit too
  d3.select('#subbtn').attr('style', 'margin-top:'+core.randi(0,20)+'px;');
}

var genProblem = function() {
  var div = d3.select('#area');
  div.html('');

  // click-option
  div.append('div').attr('id', 'boxes')
  var div_option = d3.select('#boxes');
  div_option.html('');
  var correctElement = createElements(div_option);

  // enter-text
  div.append('br')
  div.append('div').attr('id', 'form')
  var div_form = d3.select('#form');
  div_form.html('');
  div_form.append('input').attr('type', 'text').attr('id', 'tt')
  d3.select('#tt')[0][0].value ='';
  randomizeInputs();
  var q = core.sample(ui_utils.FIFTY_NAMES);

  div.append('br')
  div.append('button').html('Submit').attr('id', 'subbtn').attr('class', 'secondary-action');
  d3.select('#query').html('Select ' + correctElement.query + ', and then enter "<span class="bold">' + q + '</span>" into the text field and press Submit.');
  // d3.select('#query').html('Enter "<span class="bold">' + q + '</span>" into the text field and press Submit, after selecting ' + correctElement.query + '.');

  d3.select('#subbtn').on('click', function(){
    var t = d3.select('#tt')[0][0].value;
    var text_r = t === q ? 1.0 : -1.0;
    var option_r = d3.select('#ch'+correctElement.index)[0][0].checked ? 1.0 : -1.0;
    if (option_r == 1.0) {
        core.endEpisode(text_r, text_r > 0);
    } else {
        core.endEpisode(-1.0);
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
  <div id="wrap">
    <div id="query"></div>
    <div id="area"></div>
  </div>
</body>
</html>
